package cn.angis.quartz.controller;

import cn.angis.common.annotation.SLog;
import cn.angis.common.constant.DatePattern;
import cn.angis.common.model.R;
import cn.angis.db.controller.BaseController;
import cn.angis.db.model.PageModel;
import cn.angis.db.model.PageOfResult;
import cn.angis.quartz.dto.input.JoblogInput;
import cn.angis.quartz.dto.output.JoblogOutput;
import cn.angis.quartz.dtomapper.JoblogStruct;
import cn.angis.quartz.model.Joblog;
import cn.angis.quartz.service.JoblogService;
import com.alibaba.excel.EasyExcel;
import org.noear.snack.core.utils.DateUtil;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Post;
import org.noear.solon.core.handle.DownloadedFile;
import org.noear.solon.validation.annotation.NotBlank;
import org.noear.solon.validation.annotation.NotEmpty;
import org.noear.solon.validation.annotation.Validated;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Date;
import java.util.List;

/**
 * 定时任务调度日志表前端控制器
 *
 * @author angis.cn
 * @Date 2025-06-04
 */
@SLog("定时任务调度日志表")
@Mapping("/quartz/joblog/")
@Controller
public class JoblogController extends BaseController<JoblogService, Joblog> {

    /**
     * 分页查询
     *
     * @param joblogInput
     * @param pageModel
     * @return R<PageOfResult < JoblogOutput>>
     * @Date 2025-06-04
     */
    @SLog
    @Get
    @Mapping("page")
    public R<PageOfResult<Joblog>> page(JoblogInput joblogInput, PageModel pageModel) {
        return success(pageListByEntity(joblogInput, pageModel));
    }

    @Mapping("export")
    @Post
    public DownloadedFile export(JoblogInput joblogInput) throws FileNotFoundException {
        List<JoblogOutput> list = baseService.list(joblogInput);
        File file = new File("/upload/temp/joblog" + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN) + ".xlsx");
        EasyExcel.write(file, JoblogOutput.class).sheet("调度日志").doWrite(list);

        return new DownloadedFile("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", new FileInputStream(file), "调度日志" + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN) + ".xlsx");
    }

    /**
     * 保存
     *
     * @param joblogInput
     * @return R<Boolean>
     * @Date 2025-06-04
     */
    @SLog
    @Post
    @Mapping("save")
    public R<Boolean> save(@Validated JoblogInput joblogInput) {
        Joblog joblog = JoblogStruct.INSTANCE.toJoblog(joblogInput);
        return super.save(joblog);
    }

    /**
     * 修改
     *
     * @param joblogInput
     * @return R<Boolean>
     * @Date 2025-06-04
     */
    @SLog
    @Post
    @Mapping("update")
    public R<Boolean> update(@Validated JoblogInput joblogInput) {
        Joblog joblog = JoblogStruct.INSTANCE.toJoblog(joblogInput);
        return super.update(joblog);
    }

    /**
     * 删除
     *
     * @param id
     * @return R<Boolean>
     * @Date 2025-06-04
     */
    @SLog
    @Post
    @Mapping("delete")
    public R<Boolean> delete(@NotBlank String id) {
        return super.delete(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return R<Boolean>
     * @Date 2025-06-04
     */
    @SLog
    @Post
    @Mapping("deleteBatch")
    public R<Boolean> deleteBatch(@NotEmpty List<String> ids) {
        return super.deleteBatch(ids);
    }

    @Mapping("clean")
    public R<String> clean() {
        baseService.cleanJobLog();
        return success();
    }
}
